<template>
	<!--<el-button type="text" @click="show = true">点击打开 Dialog</el-button>-->
	<el-dialog
	  title="编辑角色"
	  :visible.sync="show1"
	  width="30%"
	  :before-close="handleClose">
	  			<el-row>
	  				<el-col ref="rolename" :span="4">角色名称</el-col>
	  				<el-col :span="20">
	  					<el-input v-model="role.roleName"></el-input>
	  				</el-col>
	  			</el-row>
	  			<el-row>
	  					<el-tree
						 :data="allPermissions"
                        show-checkbox
                        node-key="permissionDesc"
                        ref="tree"
                        :props="treeData">
						</el-tree>
	  			</el-row>
	  		</table>
	    <el-button @click="handleClose">取 消</el-button>
	    <el-button type="primary" @click="$parent.show1 = false,updateRolePermissions()">确 定</el-button>
	  </span>
	  <!--{{allPermissions}}-->
	  <!--{{allRole}}-->
	  <!--{{role}}-->
	  <!--{{allpermiss}}-->
	</el-form>
	</el-dialog>
</template>
<script>
  export default {
  	props:["show1",'role'],
    data() {
      return {
      	treeData: {
          label: 'permissionDesc',
          children: 'children'
       },
       refs:{}
      } 
    },
    computed:{
    	 allPermissions(){
            var arr=[]; 
            var obj= this.$store.getters.allPermission;
//          console.log(obj)
            for(var key in obj){
            	arr.push(obj[key]);
            }
            return arr
      },
    
    },
    mounted(){
		this.$store.dispatch("loadallpermissions")
 		},
    methods: {
      handleClose() {
        this.$parent.show1 = false;
      },
        updateRolePermissions(){
        	var permissions=[];
        	//getCheckedNodes  单词不要写错
        	this.$refs.tree.getCheckedNodes().forEach((item)=>{
        		console.log("aa")
        		permissions.push(item._id)
        	})
        	var params={
        		permissions:permissions,
        		roleName:this.role.roleName,
        		id:this.role._id
        	}
        	this.$http.post(this.$api.bainjiRole,params).
      			then((res)=>{
      				console.log(res)
      		})
        }
     },
     watch:{
     	role(newValue){
     			setTimeout(()=>{
     			var newPermission=[];
     			console.log(this.allPermissions);
     			this.allPermissions.forEach((item)=>{
     				if(newValue.permissions.indexOf(item._id)!=-1){
     					newPermission.push(item)
     				}
     			})
     			this.$refs.tree.setCheckedNodes(newPermission)
     		},0)
     	}
     }
  }
</script>
<style lang="scss">
	#addnewjuese{
		
	}
</style>